<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>HelloWorld</title>
    <script type="text/javascript" src="../assets/js/vue.js"></script>
</head>
<body>
    <h1>Hello World!</h1>
    <hr>
    <div id="app">
        {{message}}
        <ul>
            <li v-for="item in sortItems">
                {{item}}
            </li>
        </ul>
        <hr>
        <ul>
            <li v-for="(student,index) in sortStudents">
              {{index+1}} : {{student.name}} - {{student.age}}
            </li>
        </ul>
    </div>
    <script type="text/javascript">
        var app = new Vue({
            el:'#app',
            data:{
                message:'Hello World111!',
                items : [25,36,22,19,8,6,-13],
                students : [
                    {name : "JSPang",age:33},
                    {name : "Panda",age:28},
                    {name : "Panpan",age:23},
                    {name : "King",age:13}
                ]
            },
            computed:{
                sortItems : function(){
                    //这里的排序是字符串排序,而不是数字排序
                    return this.items.sort(sortNumber);
                },
                sortStudents:function(){
                    return sortByKey(this.students,'age');
                }
            }
        });

        //数字排序
        function sortNumber(a,b){
            return a-b;
        }

        //数组排序
        function sortByKey(array,key){
            return array.sort(function(a,b){
                var x=a[key];
                var y=b[key];
                return ((x<y)?-1:((x>y)?1:0));
            })
        }
    </script>
</body>
</html>